from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import os.path

# 准备好要添加的cookie的文件
cookies = [
    {'name': 'Coremail', 'value': 'f64c44113646a%YDpJHbkwvHAheMaHwdwwgGuaTCwpPEYM%g1a150.mail.163.com'},
    {'name': 'MAIL_ENTRY_INFO',
     'value': '1|0|mail163|mail163_letter|183.94.133.98|4255a1fe97d52e05f11254db15a5fb29_v1|'},
    {'name': 'NTES_SESS',
     'value': '7_fJVezhyCbrS6rG4flkcWi2fH3y9x7KWtitCF8Ea3I1Z2qhZLps8NKDKIjNVT25xj9EZki6E1cmjinT1E0f4Ufuyz9u.DPluwUY9ud_5pSJef85_6x_9C7H.XrHeYC4IqpC1.yMlzZC8JXo8_SFMKEc3kZkdFF0._HK2O9Q99ZM2mp0_ENlT9mnDFMFggQx5YtB9aufvCO1N'},
    {'name': 'MAIL_ENTRY_CS', 'value': '7af0539ca65003c2e5f93df22419e742'},
    {'name': 'cm_last_info',
     'value': 'dT1hYmN0YWcyMDIyJTQwMTYzLmNvbSZkPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0RZRHBKSGJrd3ZIQWhlTWFId2R3d2dHdWFUQ3dwUEVZTSZzPVlEcEpIYmt3dkhBaGVNYUh3ZHd3Z0d1YVRDd3BQRVlNJmg9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20lMkZqczYlMkZtYWluLmpzcCUzRnNpZCUzRFlEcEpIYmt3dkhBaGVNYUh3ZHd3Z0d1YVRDd3BQRVlNJnc9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20mbD0tMSZ0PS0xJmFzPXRydWU='}
]


# 打开浏览器输入163网址
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.163.com/')

# 添加cookie
for i in range(len(cookies)):
    driver.add_cookie(cookies[i])
# 刷新浏览器
driver.refresh()

time.sleep(2)
# 定位写信界面点击写信
driver.find_element(By.ID, '_mail_component_149_149').click()
# 定位收件人页面点击输入收件人
ipt = driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt')
ipt.send_keys('525695538@qq.com')
# 定位主体页面输入主题
driver.find_element(By.XPATH,'//header/div[2]/div[1]/div/div/input').send_keys('打王者')

# 添加附件
time.sleep(2)
file = os.path.dirname(os.path.abspath(__file__)) + '/html'
driver.find_element(By.CLASS_NAME, 'O0').send_keys(file)

# 切换到输入内容框架输入内容
time.sleep(2)
iframe = driver.find_elements(By.TAG_NAME, 'iframe')[3]
driver.switch_to.frame(iframe)
driver.find_element(By.CLASS_NAME, 'nui-scroll').send_keys('上号')

# 回到主框架点击发送
time.sleep(2)
driver.switch_to.default_content()
driver.find_element(By.XPATH, '//div[2]/div[1]/div[2]/header/div/div[1]/div/span[2]').click()

# 退出浏览器
time.sleep(2)
driver.quit()


